package it.slkfarm.http;

import it.slkfarm.bean.Farmer;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.ByteArrayBuffer;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;

import android.provider.OpenableColumns;
import android.util.Log;

public class HttpConnector 
{
	/**
	 * @uml.property  name="inputStream"
	 */
	private InputStream inputStream; 	
	/**
	 * @uml.property  name="jsonObject"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.lang.String"
	 */
	private JSONObject jsonObject;
	/**
	 * @uml.property  name="jsonString"
	 */
	private String jsonString;
	/**
	 * @uml.property  name="client"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private DefaultHttpClient client;
	/**
	 * @uml.property  name="post"
	 * @uml.associationEnd  
	 */
	private HttpPost post;
	/**
	 * @uml.property  name="response"
	 * @uml.associationEnd  
	 */
	private HttpResponse response;
	/**
	 * @uml.property  name="entity"
	 * @uml.associationEnd  
	 */
	private HttpEntity entity;
	/**
	 * @uml.property  name="reader"
	 */
	private BufferedReader reader;
	
	public HttpConnector()
	{
		client = new DefaultHttpClient();
	}
	
	public JSONObject getJson() throws IllegalStateException, IOException, JSONException
	{
		entity = response.getEntity();
		inputStream = entity.getContent();
		reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) 
		{
			sb.append(line + "\n");
		}
		inputStream.close();
		reader.close();
		jsonString = sb.toString();
		jsonObject = new JSONObject(jsonString);
		return jsonObject;
	}
	
	public int connect(String URL, List<NameValuePair> variables) throws ClientProtocolException, IOException
	{
		post = new HttpPost(URL);
		if (variables != null) 
			post.setEntity(new UrlEncodedFormEntity(variables));
		response = client.execute(post);
		return response.getStatusLine().getStatusCode();
	}
	
	public void getImage(String urlImage, String name) throws IOException
	{
		if (!urlImage.equalsIgnoreCase("Not Available"))
		{
			File file = new File("/data/data/it.slkfarm.android/files/"+name.toLowerCase()+".png");
			if (!file.exists())
			{
				file.createNewFile();
				urlImage = urlImage.replaceAll("\\\\" , "//");
				urlImage = urlImage.replaceAll(" " , "%20");
				InputStream is = (InputStream) new URL(urlImage).getContent();
				Bitmap b = BitmapFactory.decodeStream(is);
				b = Bitmap.createScaledBitmap(b, 48, 48, false);
				Log.i("size", String.valueOf(b.getWidth()));
				ByteArrayOutputStream bytes = new ByteArrayOutputStream();
				b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
				Bitmap.createScaledBitmap(b, 48, 48, false);
				FileOutputStream fo = new FileOutputStream(file);
				fo.write(bytes.toByteArray());
				fo.close();
			}
		}	
	}
}
